package com.tysong.qq.listener;

import com.google.common.collect.Lists;
import com.tysong.qq.service.CaiHongPiService;
import com.tysong.qq.service.ChengyuService;
import com.tysong.qq.service.TianQiService;
import com.tysong.qq.service.TuPianService;
import love.forte.common.ioc.annotation.Beans;
import love.forte.simbot.annotation.Filter;
import love.forte.simbot.annotation.Listen;
import love.forte.simbot.api.message.events.GroupMemberIncrease;
import love.forte.simbot.api.message.events.GroupMsg;
import love.forte.simbot.api.message.events.PrivateMsg;
import love.forte.simbot.api.sender.MsgSender;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

@Beans
public class GroupListener {

    //只处理哪些qq群
    private static List<String> processGroupNumber= Lists.newArrayList();
    static {
        processGroupNumber.add("1159483152");
    }


    /**
     * 私信消息
     */
    @Listen(PrivateMsg.class)
    public void privateMsg(PrivateMsg msg, MsgSender sender){
        System.out.println("收到消息：" + msg);
    }


    /**
     * 群消息
     *  @Filter(at=true) 只要at我的信息
     * @param msg
     * @param sender
     */
    @Listen(GroupMsg.class)
    @Filter(anyAt=true)
    public void groupMsg(GroupMsg msg, MsgSender sender){
        String groupNumber=msg.getGroupInfo().getGroupCode();
        if (!processGroupNumber.contains(groupNumber)){
            return;
        }
        if (StringUtils.contains(msg.getMsg(),"天气")){
            System.out.println("收到消息：" + msg);
            String[] data = msg.getMsg().split(" ");
            String city = data[1].replaceAll("天气","");
            String weather = TianQiService.INSTANCE.getWeather(city);
            sender.SENDER.sendGroupMsg(groupNumber,weather);
            return;
        }
        if (StringUtils.contains(msg.getMsg(),"图片")){
            System.out.println("收到消息：" + msg);
            String[] data = msg.getMsg().split(" ");
            String word = data[1].replaceAll("图片","");
            String msg1 = TuPianService.INSTANCE.msg("http://b.zol-img.com.cn/desk/bizhi/image/1/1680x1050/1349289433496.jpg");
            sender.SENDER.sendGroupMsg(groupNumber,msg1);
            return;
        }
        if (StringUtils.contains(msg.getMsg(),"夸我")){
            System.out.println("收到消息：" + msg);
            String[] data = msg.getMsg().split(" ");
            String msg1 = CaiHongPiService.INSTANCE.caihongpi("");
            sender.SENDER.sendGroupMsg(groupNumber,msg1);
            return;
        }
        if (StringUtils.contains(msg.getMsg(),"功能")||StringUtils.contains(msg.getMsg(),"菜单")){
            String outText="当前助手支持功能:"+"\n"
                    +"1、天气查询:北京天气"
                    +"2、成语接龙:一马当先"
                    +"3、图片:图片";
            sender.SENDER.sendGroupMsg(groupNumber,outText);
            return;
        }
        String[] data = msg.getMsg().split(" ");
        String chengyu = data[1];
        if (chengyu.length()==4) {
            String jieLong = ChengyuService.INSTANCE.jieLong(chengyu);
            sender.SENDER.sendGroupMsg(groupNumber, jieLong);
        }

    }

    @Listen(GroupMemberIncrease.class)
    public void groupMemAdd(GroupMemberIncrease groupMemberIncrease, MsgSender sender){
        String groupNumber=groupMemberIncrease.getGroupInfo().getGroupCode();
        if (!processGroupNumber.contains(groupNumber)){
            return;
        }
        String beOperatedQQ = groupMemberIncrease.getBeOperatorInfo().getBeOperatorCode();
        sender.SENDER.sendGroupMsg(groupNumber,"欢迎"+beOperatedQQ+"加入本群");
    }
}
